/*
#-----------------------------------------------------------------------------
# Name:        zlibw.h
# Purpose:     zlib wrapper
#
# Author:      Seiji Morikami
#
# Created:     2005/13/02
# Copyright:   (c) 2005
# Licence:     See stegppm.license.txt
#-----------------------------------------------------------------------------
*/

#ifndef ZLIBW_H
#define ZLIBW_H

#include "zlib.h"
#include "typedef.h"

namespace seiji
{

vuc_t zlib(vuc_t& bf)
{
    z_stream zst;
    vuc_t cbf(2*bf.size());

    zst.next_in = &bf[0];
    zst.avail_in = bf.size();
    zst.next_out = &cbf[0];
    zst.avail_out = 2*bf.size();
    zst.zalloc = 0;
    zst.zfree = 0;
    zst.opaque = 0;

    deflateInit2(&zst,9,Z_DEFLATED,15,9,Z_DEFAULT_STRATEGY);
    deflate(&zst,Z_FINISH);
    cbf.resize(zst.total_out);
    deflateEnd(&zst);

    return cbf;
}

vuc_t unzlib(vuc_t& cbf, int destl)
{
    z_stream zst;
    vuc_t bf(destl);

    zst.next_in = &cbf[0];
    zst.avail_in = cbf.size();
    zst.next_out = &bf[0];
    zst.avail_out = destl;
    zst.zalloc = 0;
    zst.zfree = 0;
    zst.opaque = 0;

    inflateInit(&zst);
    inflate(&zst,Z_FINISH);
    bf.resize(zst.total_out);
    inflateEnd(&zst);

    return bf;
}

}
#endif
